<?php

class Signup extends CI_Controller {

	public function index(){
		//Determine if the user is loged in.
		if ($this->session->userdata('user_logedin')) {
			//If the user is loged in then redirect to start page.
			redirect('start');
		}
		else {
			//Loading basic templates.
			$this->load->view('templates/v_header');
			$this->load->view('templates/v_menu_basic');
			
			//Loading validation library.
			$this->load->library('form_validation');
			
			//Setting the delimiters for error messages so they can be styled by CSS.
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
			
			//Setting validation rules.
			$this->form_validation->set_rules('username', 'username', 'required|is_unique[user.username]|alpha_dash|min_length[3]|max_length[45]');
			$this->form_validation->set_rules('password1', 'password', "required|callback__password_check|matches[password2]");
			$this->form_validation->set_rules('email1', 'e-mail', 'required|valid_email|is_unique[user.email]|matches[email2]|max_length[255]');
			$this->form_validation->set_rules('agreement', 'terms of use', 'callback__agreement_check');
			
			//Customising the error messages.
			$this->form_validation->set_message('is_unique', 'The selected %s is already in use.');
			$this->form_validation->set_message('required', 'You have to enter your %s.');
			$this->form_validation->set_message('matches', "The %s doesn't match the retype field.");
			
			//Determine if the entered data was OK.
			if ($this->form_validation->run() == FALSE) {
				//Load form helper.
				$this->load->helper('form');
				
				//Repopulating the form.
					$repopulation = array(	'username'	=> set_value('username'),
											'email1'	=> set_value('email1'),
											'email2'	=> set_value('email2'),
											);
					
					//Putting the entered userdata into the page variable.
					$page_data['repopulation'] = $repopulation;
				//End repopulating the form
				
				//Loading the attributs for the popup window for the terms of use.
				$page_data['popup_attributes'] = array(	'width'			=> '800',
														'height'		=> '600',
														'scrollbars'	=> 'yes',
														'status'		=> 'yes',
														'resizable'		=> 'yes',
														'screenx'		=> '0',
														'screeny'		=> '0'
														);
				
				//Creating help images
					//Fetching information about passord security.
					$password_security = $this->config->item('password_security');
					
					//Sending help images to the view.
					$page_data['help_images'] = array(	'username'	=> $this->namustar->help_image('The username can only contain letters (A-Z), numbers (0-9), underscores and dashes. It has to be at least 3 characters long but no longer than 45 characters.'),
														'email'		=> $this->namustar->help_image('The e-mail adress has to be a valid one as we have to send an activation link to you.'),
														'password'	=> $this->namustar->help_image('Passwords has to be ' . minimum_password_length() . ' characters long and contain a minimum of ' . $password_security['big_letters'] . ' big letters, ' . $password_security['small_letters'] . ' small letters, ' . $password_security['numbers'] . ' numbers and ' . $password_security['special_characters'] . ' special carachers.') );
				
				//Load page for signup form.
				$this->load->view('pages/v_signup', $page_data);
			}
			else {
				//Loading parameters needed to create the user.
				$parameters['username']	= $this->input->post('username');
				$parameters['password']	= $this->input->post('password1');
				$parameters['email']	= $this->input->post('email1');
				
				//Creating an user object.
				$this->load->model('m_user', 'user');
				
				//Creating the user.
				$success = $this->user->create($parameters);
				
				//Determine what message to show based on the result from the user creation.
				if ($success) {
					$page_data['resend_link'] = anchor('activation/resend', 'resend activation link');
					
					$this->load->view('pages/v_signup_success', $page_data);
				}
				else {
					$this->load->view('pages/v_signup_fail');
				}
			}
			
			//Loading page footer.
			$this->load->view('templates/v_footer');
		}
	}
	
	public function terms_of_use() {
		//Loading the terms of use view.
		$this->load->view('pages/v_terms_of_use');
	}
	
	public function _agreement_check($agreement) {
		//Checking if the user agreeded to the terms of use.
		if ($agreement != FALSE) {
			return TRUE;
		}
		else {
			//Setting a error message for the callback.
			$this->form_validation->set_message('_agreement_check', 'You have to agree to the %s to use this web site.');
			
			return FALSE;
		}
	}
	
	public function _password_check($password) {
		//Check if the password is safe enough.
		$result = check_password_security($password);
		
		if ($result['bool']) {
			return TRUE;
		}
		else {
			//Setting a error message for the callback.
			$this->form_validation->set_message('_password_check', $result['message']);
			
			return FALSE;
		}
	}
}
?>